/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.model.service;

import com.model.domain.dinero.Partida;
import com.model.domain.dinero.ConceptoPagoPersonal;
import com.model.domain.dinero.ConglomeradoPersonalDetalle;
import com.model.domain.persona.Cargo;
import com.model.domain.persona.Personal;
import com.model.domain.ubicacion.Conglomerado;
import com.model.domain.ubicacion.AreaGeografica;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import util.www.jdbc.JdbcUtil;
import util.www.jdbc.OperadorResulSet;
import util.www.jdbc.SeteadorJdbc;


/**
 *
 * @author Rdelacruz
 */
public class ConceptoPagoPersonalService 
extends AbstractService
{
    
    private ConglomeradoService conglomeradoService;
    private PersonalService personalService;
    private PartidaService partidaService;

    public ConceptoPagoPersonal findByFiltro(Connection con, ConglomeradoPersonalDetalle filtro) throws SQLException {
        
        
        Conglomerado c = conglomeradoService.findByCodigo(con, 
                filtro.getConglomeradoPersonalResumen().getConglomerado().getCodigo(),
                new SeteadorJdbc<Conglomerado>() {
                    @Override
                    public void setear(Connection con, ResultSet rs, Conglomerado obj) throws SQLException {
                        conglomeradoService.setearSede(rs, obj);
                        conglomeradoService.setearOdei(con, rs, obj);
                    }
                }
                );//con sede
        
        Personal p = personalService.findByDni(con, 
                filtro.getConglomeradoPersonalResumen().getPersonal().getDni(), 
                new SeteadorJdbc<Personal>() {
                    @Override
                    public void setear(Connection con, ResultSet rs, Personal obj) throws SQLException {
                        personalService.setearCargo(con, rs, obj);
                    }
                });//con cargo
        
        Partida pa = partidaService.findByCodigo(con, filtro.getPartida().getCodigo());
        
        boolean esLocacionServicios = true;
        String codigo= "";
        
        //VIATICOS
        if(pa.getCodigo().equals("232122U") || pa.getCodigo().equals("232122R")) {
            if(esLocacionServicios) {
                if(c.getAreaGeografica()==AreaGeografica.RESTO_URBANO)
                    codigo = "2301";
                else if(c.getAreaGeografica()==AreaGeografica.RURAL)
                    codigo = "2302";
            }
        }
        //MOVILIDAD LOCAL
        else if(pa.getCodigo().equals("2321299")) {
            boolean esDeLima = c.getOdei().getDepartamento().isLima();
            Cargo cg = p.getCargo();
            if(cg.isEncuestador()) {
                if(esDeLima)
                    codigo = "2101";
                else
                    codigo = "2102";
            }
            else if(cg.isSupervisorLocal()) {
                if(esDeLima)
                    codigo = "2103";
                else
                    codigo = "2104";
            }
            else if(cg.isCoordinador() || cg.isSupervisorNacional()) {
                if(esDeLima)
                    codigo = "2105";
                else
                    codigo = "2106";
            }
        }
        
        final ConceptoPagoPersonal cp = new ConceptoPagoPersonal();
        
        String sql = "select * from Concepto_Pago_Personal where codigo='"+codigo+"'";
        JdbcUtil.ejecutarUnaVez(con, sql, new OperadorResulSet() {
            @Override
            public void operar(Connection con, ResultSet rs) throws SQLException {
                cp.setMonto(rs.getInt("monto"));
            }
        });
        return cp;
    }

    public ConglomeradoService getConglomeradoService() {
        return conglomeradoService;
    }

    public void setConglomeradoService(ConglomeradoService conglomeradoService) {
        this.conglomeradoService = conglomeradoService;
    }

    public PersonalService getPersonalService() {
        return personalService;
    }

    public void setPersonalService(PersonalService personalService) {
        this.personalService = personalService;
    }

    public PartidaService getPartidaService() {
        return partidaService;
    }

    public void setPartidaService(PartidaService partidaService) {
        this.partidaService = partidaService;
    }

    
    
    
    
}
